package com.tangcco.lockwell.controller.admin;

import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.domain.UserEventLog;
import com.tangcco.lockwell.domain.other.KeyValue;
import com.tangcco.lockwell.service.UserEventLogService;
import com.tangcco.lockwell.service.impl.UserEventLogServiceImpl;
import com.tangcco.lockwell.utility.DateTimeUtil;
import com.tangcco.lockwell.viewmodel.admin.user.IndexVM;
import com.tangcco.lockwell.viewmodel.admin.user.UserEventPageRequestVM;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/api/admin/user")
public class UserEventLogController {

    private final UserEventLogService userEventLogService;

    public UserEventLogController(UserEventLogService userEventLogService) {
        this.userEventLogService = userEventLogService;
    }

    // 创建一个用于分页查询用户事件日志的接口
    @PostMapping("/event/page/list")
    public RestResponse<PageInfo<UserEventLog>> pageUserEventLogs(@RequestBody UserEventPageRequestVM userEventPageRequestVM) {
        // 创建请求参数对象，将从前端接收的分页参数传入

        // 调用服务层的page方法进行分页查询
        PageInfo<UserEventLog> pageInfo = userEventLogService.page(userEventPageRequestVM);
        return RestResponse.ok(pageInfo);
    }
    @PostMapping("/index")
    public RestResponse<IndexVM> selectMothCount () {
        IndexVM indexVM = new IndexVM();
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("yyyy");
        String currentYear = now.format(yearFormatter);
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MM");
        String currentMonth = now.format(monthFormatter);

        List<Integer> mothDayUserActionValue = userEventLogService.selectMothCount(currentYear, currentMonth);
        List<Integer> mothDayDoExamQuestionValue = userEventLogService.selectQuestionMothCount(currentYear, currentMonth);

        indexVM.setMothDayUserActionValue(mothDayUserActionValue);
        indexVM.setMothDayDoExamQuestionValue(mothDayDoExamQuestionValue);
        indexVM.setMothDayText(DateTimeUtil.MothDay());
        return RestResponse.ok(indexVM);
    }

}